import { _decorator, AnimationClip, Animation } from 'cc';
import { EntityManager } from '../../Base/EntityManager';
import State from '../../Base/State';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../Base/StateMachine';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enums';
import BlockfrontSubStateMachine from './BlockfrontSubStateMachine';
import BlockturnLeftSubStateMachine from './BlockturnLeftSubStateMachine';
import BlockturnRightSubStateMachine from './BlockturnRightSubStateMachine';
import IdleSubStateMachine from './IdleSubStateMachine';
import TurnLeftSubStateMachine from './TurnLeftSubStateMachine';
import TurnRightSubStateMachine from './TurnRightSubStateMachine';
const { ccclass, property } = _decorator;

/**
 * 有限状态机 fsm 玩家实例
 */

@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {

  async init() {
    this.animationComponent = this.addComponent(Animation)

    this.initParams()
    this.initStateMachines()
    this.initAnimationEvent()

    // 等到所有资源都加载完成，再退出 init 方法
    await Promise.all(this.waitingList)
  }

  /**
   * 初始化状态机的参数列表
   * @ DIRECTION【角色朝向】: number
   * @ IDLE【待机状态】: trigger
   * @ TURNLEFT【角色左转】: trigger
   * @ BLOCKFRONT【移动碰撞】: trigger
   * @ BLOCKTURNLEFT 【左转碰撞】: trigger
   *
  */
  initParams() {
    this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.TURNLEFT, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.TURNRIGHT, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.BLOCKFRONT, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.BLOCKTURNRIGHT, getInitParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber())
  }

  // 初始化状态机
  initStateMachines() {
    // 注册子状态机
    this.states.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this))
    this.states.set(PARAMS_NAME_ENUM.TURNLEFT, new TurnLeftSubStateMachine(this))
    this.states.set(PARAMS_NAME_ENUM.TURNRIGHT, new TurnRightSubStateMachine(this))
    this.states.set(PARAMS_NAME_ENUM.BLOCKFRONT, new BlockfrontSubStateMachine(this))
    this.states.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, new BlockturnLeftSubStateMachine(this))
    this.states.set(PARAMS_NAME_ENUM.BLOCKTURNRIGHT, new BlockturnRightSubStateMachine(this))
  }

  initAnimationEvent() {
    // 播放完添加一个回调
    this.animationComponent.on(Animation.EventType.FINISHED, ()=> {
      // 判断一下当前的状态
      const name = this.animationComponent.defaultClip.name
      const whiteList = ['block','turn']
      // 如果是 TURN 转身类型的状态，播放完动画就切回 IDLE 状态
      if (whiteList.some(v=> name.includes(v))) {
        this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
        // this.setParams(PARAMS_NAME_ENUM.IDLE, true)
      }
    })
  }
  // 状态机的执行方法就是 状态间的切换，改变 当前状态 currentState
  run() {
    switch (this.currentState) {
      case this.states.get(PARAMS_NAME_ENUM.TURNLEFT):
      case this.states.get(PARAMS_NAME_ENUM.TURNRIGHT):
      case this.states.get(PARAMS_NAME_ENUM.IDLE):
      case this.states.get(PARAMS_NAME_ENUM.BLOCKFRONT):
      case this.states.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT):
      case this.states.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT):
        if (this.params.get(PARAMS_NAME_ENUM.BLOCKFRONT).value) {
          this.currentState = this.states.get(PARAMS_NAME_ENUM.BLOCKFRONT)
        } else if (this.params.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT).value) {
          this.currentState = this.states.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT)
        } else if (this.params.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT).value) {
          this.currentState = this.states.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT)
        } else if (this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value) {
          // 参数为向左转
          // 从状态机数组中取出这个 State
          this.currentState = this.states.get(PARAMS_NAME_ENUM.TURNLEFT)
        } else if (this.params.get(PARAMS_NAME_ENUM.TURNRIGHT).value) {
          // 参数为向右转
          // 从状态机数组中取出这个 State
          this.currentState = this.states.get(PARAMS_NAME_ENUM.TURNRIGHT)
        }else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
          // 参数为待机
          // 从状态机数组中取出这个 State
          this.currentState = this.states.get(PARAMS_NAME_ENUM.IDLE)
        } else {
          this.currentState = this.currentState
        }
        break

      default:
        this.currentState = this.states.get(PARAMS_NAME_ENUM.IDLE)
    }
  }
}
